package com.roy.feign;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.roy.eureka.api.dto.User;

import feign.Feign;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import feign.okhttp.OkHttpClient;

public class BasicFeignMain {

	public static void main(String[] args) {
		//1、普通接口访问
		IUserService basicService = Feign.builder()
				.target(IUserService.class, "http://localhost:8081");
		System.out.println(basicService.hello());
		System.out.println(basicService.getStringInfo("BasicFeign"));
		//2、结果转码器
		IUserService gsonService = Feign.builder()
				.decoder(new GsonDecoder())
				.target(IUserService.class, "http://localhost:8081");
		List<User> users = gsonService.getUsers(4);
		for(User user:users) {
			System.out.println(JSON.toJSONString(user));
		}
		//3、参数转码器
		IUserService gsonService2 = Feign.builder()
				.encoder(new GsonEncoder())
				.decoder(new GsonDecoder())
				.target(IUserService.class, "http://localhost:8081");
		User user = new User();
		User oneUser = gsonService2.getOneUser(user);
		System.out.println(JSON.toJSONString(oneUser));
		//4、客户端-注意要是feign包中的客户端
		IUserService gsonService3 = Feign.builder()
				.client(new OkHttpClient())
				.encoder(new GsonEncoder())
				.decoder(new GsonDecoder())
				.target(IUserService.class, "http://localhost:8081");
		User oneUser2 = gsonService3.getOneUser(user);
		System.out.println(JSON.toJSONString(oneUser2));
		//5、注解翻译器
	}
}
